package com.apes.auto.car.model;

import com.apes.auto.car.repository.VehicleTypeRepository;
import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.Arrays;
import java.util.List;

/**
 * @ClassName VehicleType
 * @Description TODO
 * @Author pengyb
 * @Date 2019/7/10 18:07
 * @Version 1.0
 **/
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = VehicleType.CONST_TABLE_NAME,indexes = {
        @Index(columnList = "name"),
        @Index(columnList = "name,car_series_id")
})
public class VehicleType extends Model {
    public static final String CONST_TABLE_NAME = "dc_vehicle_type";

    @Id
    @Column(name = "id", length = 30)
    @Comment("车型编码")
    private String id;  //编码

    @Column(name = "name", length = 250)
    @Comment("车型名称")
    private String name;// 名称  ==车系+车型

    @Column(name = "sequence", length = 2)
    @Comment("车型序号")
    private String sequence; //车型序号

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "vehicleLabel_id", referencedColumnName = "id")
    @Comment("厂牌信息")
    private VehicleLabel vehicleLabel;

    @ManyToOne
    @JoinColumn(name = "car_series_id", referencedColumnName = "id")
    @Comment("车系")
    private CarSeries carSeries;


    //检查序号
    public String checkSequence(String vehicleLabelID) {
        String[] splitBet = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}; //数字组合
        List<String> splitBetList = Arrays.asList(splitBet);

        String[] monogram = {"A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P","R", "S", "T", "U", "V", "W", "X", "Y"};
        List<String> monogramList = Arrays.asList(monogram);

        String maxSequence = SpringManager.getBean(VehicleTypeRepository.class).findByMAXSequence(vehicleLabelID);
        if (maxSequence != null) {
            String s1 = maxSequence.substring(0, 1);
            String s2 = maxSequence.substring(1, 2);

            if (s2.charAt(0) < 57) { //s2<9
                s2 = splitBetList.get(splitBetList.indexOf(maxSequence.substring(1, 2)) + 1);
            } else if (maxSequence.charAt(1) == 57) { //s2==9
                s2 = "A";
            } else if (maxSequence.charAt(1) > 57) { //s2>9
                if (maxSequence.substring(1, 2).equals("Y")) {
                    s2 = "0";

                    if (maxSequence.charAt(0) < 57) { //s1<9
                        s1 = splitBetList.get(splitBetList.indexOf(maxSequence.substring(0, 1)) + 1);
                    } else if (maxSequence.charAt(0) == 57) {//s1==9
                        s1 = "A";
                    } else if (maxSequence.charAt(0) > 57) { //s1>9
                        if (maxSequence.substring(0, 1).equals("Y")) {
                            throw new RuntimeException("序号超过2位数");
                        }
                        s1 = monogramList.get(monogramList.indexOf(maxSequence.substring(0, 1)) + 1);
                    }

                } else {
                    s2 = monogramList.get(monogramList.indexOf(maxSequence.substring(1, 2)) + 1);
                }
            }


            return s1 + s2;
        } else {
            return "00";
        }
    }
}
